package com.saifan.wyy_ov.ui.onlishop;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.CommonDataBean;
import com.saifan.wyy_ov.data.bean.StoreBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.IGetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

/**
 * @author WangLi
 * @version V1.0
 * @date 2015年3月30日 下午1:03:40
 * @Description 我的收藏页面
 */

public class MyCollectActivity extends BaseActivity {

    private ListView listView;
    RefreshLayout refreshLayout;
    private RelativeLayout loadlay;
    private int page = 0;// 第几页
    private CommonAdapter<StoreBean> adapter;
    private List<StoreBean> beans=new ArrayList<>();
    private IGetData getData;
    private CommonDataBean requestBean;
    private Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        findViews();
        requestBean = new CommonDataBean();
        requestBean.setYXDID(getRoomInfoBean().getXMBS());
        requestBean.setCustomerID(getRoomInfoBean().getWYKHDA_ZJ());
        getData = new GetData();
        getDataInit();
    }

    // 初始化控件
    @SuppressLint("InflateParams")
    private void findViews() {
        setContentView(R.layout.base_pulltorefresh);
        loadlay = (RelativeLayout) findViewById(R.id.loading_lay);
        listView = (ListView) findViewById(R.id.listView);
        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        adapter = new CommonAdapter<StoreBean>(MyCollectActivity.this, beans, R.layout.store_item) {
            @Override
            public void convert(ViewHolderHelper helper, StoreBean item) {
                helper.setText(R.id.store_name, StringUtil.returnShow(item.getStoreName()));
                ImageView statePic = helper.getView(R.id.state_pic);
                if (item.isIsDelivery()) {
                    statePic.setVisibility(View.VISIBLE);
                } else {
                    statePic.setVisibility(View.GONE);
                }
                String date = "", time = "";
                if (!StringUtil.isNullOrEmpty(item.getBusinessDate())) {
                    date = item.getBusinessDate().replace(",", "至");
                }
                if (!StringUtil.isNullOrEmpty(item.getBusinessTime())) {
                    time = item.getBusinessTime().replace(",", "至");
                }
                helper.setText(R.id.store_time, date + " " + time);
                if (StringUtil.isNullOrEmpty(item.getDeliveryInstructions())) {
                    helper.setText(R.id.note,   "不支持外送");
                } else {
                    helper.setText(
                            R.id.note,
                            StringUtil.returnShow(String.valueOf(item.getDeliveryAmount())) + " 元起送/"
                                    + StringUtil.returnShow(item.getDeliveryInstructions()));
                }
                helper.setImageByUrl(R.id.store_img, item.getHeadPhoto());
            }
        };

        listView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                page = 0;
                refreshLayout.setRefreshing(true);
                getDataInit();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                // TODO Auto-generated method stub
                refreshLayout.setLoading(true);
                page++;
                getDataInit();

            }
        });
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Intent intent = new Intent(MyCollectActivity.this, StoreDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", beans.get(arg2));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    // 初始化数据
    private void getDataInit() {
        requestBean.setPage(page);
        if (beans.isEmpty()){
            loadlay.setVisibility(View.VISIBLE);
        }
        getData.getDataJson(MyCollectActivity.this, Api.StoreList.MYCOLLECT, requestBean, "", new ResponseData() {
            @Override
            public void onSuccess(String json) {
                loadlay.setVisibility(View.GONE);
                List<StoreBean> list = (new Gson()).fromJson(json, new TypeToken<List<StoreBean>>() {
                }.getType());
                if (list!=null&&!list.isEmpty()) {
                    if (page == 0) {
                        beans = list;
                    } else {
                        beans.addAll(list);
                    }
                } else {
                    if (page == 0) {
                        ToastUtil.showToast(MyCollectActivity.this, "暂无相关店铺信息");
                    } else {
                        ToastUtil.showToast(MyCollectActivity.this, "暂无更多相关店铺信息");
                        page--;
                    }
                }
                adapter.mDatas = beans;
                adapter.notifyDataSetChanged();
                loadlay.setVisibility(View.GONE);
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);
            }

            @Override
            public void onFailed(String str) {
                loadlay.setVisibility(View.GONE);
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);
                if (page > 0) {
                    page--;
                }

            }
        });
    }
}
